package com.offcn.sellergoods.controller;

import com.offcn.entity.PageResult;
import com.offcn.entity.Result;
import com.offcn.entity.StatusCode;
import com.offcn.sellergoods.pojo.Brand;
import com.offcn.sellergoods.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PreDestroy;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "品牌模块")
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @ApiOperation(value = "查询所有的品牌")
    @RequestMapping("/list")
    public Result<List<Brand>> findAll(HttpServletRequest request){
        // 获取请求头的数据
        String token = request.getHeader("Authorization");
        System.out.println("请求头的令牌数据:" + token);
        List<Brand> brandList = null;
        try {
            brandList = brandService.findAll();
            return new Result<>(true, StatusCode.OK,brandList,"查询所有品牌成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR,null,"查询所有品牌失败");
        }
    }

    @PreAuthorize("hasAnyAuthority('ADMIN')") // 拥有admin身份才可以访问该方法
    @ApiOperation(value = "根据ID查询品牌")
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable Long id){
        Brand brand = brandService.findById(id);
        return new Result<>(true,StatusCode.OK,brand,"查询品牌成功");
    }

    @ApiOperation(value = "添加品牌")
    @PostMapping("/")
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"新增品牌成功");
    }

    @ApiOperation(value = "修改品牌")
    @PutMapping("/{id}")
    public Result update(@PathVariable Long id,@RequestBody Brand brand){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true,StatusCode.OK,"修改品牌成功");
    }

    @ApiOperation(value = "删除品牌")
    @ApiImplicitParam(name = "id",value = "商品ID")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除品牌成功");
    }

    @PostMapping("/search")
    public Result<List<Brand>> search(@RequestBody Brand brand){
        List<Brand> list = brandService.findList(brand);
        return new Result<>(true,StatusCode.OK,list,"根据条件查询成功");
    }

    @GetMapping("/search/{pageNo}/{pageSize}")
    public Result<PageResult<Brand>> findPage(@PathVariable Integer pageNo,@PathVariable Integer pageSize){
        PageResult<Brand> pageResult = brandService.findPage(pageNo, pageSize);
        return new Result<>(true,StatusCode.OK,pageResult,"查询分页成功");
    }

    @PostMapping("/searchByBrand/{pageNo}/{pageSize}")
    public Result<PageResult<Brand>> findPage(
            @PathVariable Integer pageNo,
            @PathVariable Integer pageSize,
            @RequestBody Brand brand){
        PageResult<Brand> pageResult = brandService.findPage(brand,pageNo, pageSize);
        return new Result<>(true,StatusCode.OK,pageResult,"查询分页成功");
    }

    @GetMapping("/selectOptionBrands")
    public Result<List<Map>> selectOptionBrands(){
        List<Map> brands = brandService.selectOptionBrand();
        return new Result<>(true,StatusCode.OK,brands,"品牌列表项查询成功");
    }
}
